import UIKit
import Static

class ViewController: TableViewController {
    
    var name: String = "测试用"
    
    // 自定义头部： 这个可以定制间距和实现自定义效果
    private let emptyHeaderFooter: UIView  = {
        // 头部宽度只是参考！！！ 会自动撑满屏幕的
        let view = UIView(frame: CGRect(x: 0, y: 0, width: 320, height: 5))
        //view.backgroundColor = .redColor()
        return view
    }()

    // MARK: - Properties
    // 这么高明的写法 ，牛逼啊，进行简单的定制开发而已，一般最外层肯定是定制容器！！！
    private let customAccessory: UIView = {
        let view = UIView(frame: CGRect(x: 0, y: 0, width: 55, height: 55))
        view.backgroundColor = .redColor()
        return view
    }()


    // MARK: -  实现样式切换
    convenience init() {
        self.init(style: .Grouped)
    }


    // MARK: - UIViewController

    override func viewDidLoad() {
        super.viewDidLoad()

        title = "Static"
        
        tableView.rowHeight = 50
        tableView.estimatedRowHeight = 50

        dataSource.sections = [
            TBSection(header: "Styles", rows: [
                Row(text: "Value 1", detailText: name, rowHeight: 100, selection: { [unowned self] in
                    
                    //  三段式，非常完美的简单页面设置和实现
                    self.name = "新的值"
                    // 刷新界面
                    self.dataSource.sections[0].rows[0].detailText = "新的值"
                    self.tableView.reloadRowsAtIndexPaths([NSIndexPath(forRow: 0, inSection: 0)], withRowAnimation: UITableViewRowAnimation.Automatic)
                    
                    self.tableView.reloadData()
                    self.showAlert(title: "Row Selection")
                    }, accessory: .View(customAccessory),  cellClass: Value1Cell.self),
                Row(text: "Value 1", detailText: "with an image", cellClass: Value1Cell.self, image: UIImage(named: "Settings")),
                Row(text: "Value 2", detailText: "Detail", cellClass: Value2Cell.self),
                Row(text: "Subtitle", detailText: "Detail", cellClass: SubtitleCell.self),
                Row(text: "Button", detailText: "Detail", cellClass: ButtonCell.self, selection: { [unowned self] in
                    self.showAlert(title: "Row Selection")
                }),
                Row(text: "Custom from nib", cellClass: NibTableViewCell.self)
            ], footer: "This is a section footer."),
            TBSection(header: "Accessories", rows: [
                Row(text: "None"),
                Row(text: "Disclosure Indicator", accessory: .DisclosureIndicator),
                Row(text: "Detail Disclosure Button", accessory: .DetailDisclosureButton({ [unowned self] in
                    self.showAlert(title: "Detail Disclosure Button")
                })),
                Row(text: "Checkmark", accessory: .Checkmark),
                Row(text: "Detail Button", accessory: .DetailButton({ [unowned self] in
                    self.showAlert(title: "Detail Button")
                })),
                Row(text: "Custom View", accessory: .View(customAccessory))
            ], footer: nil),
            TBSection(header: nil, rows: [
                Row(text: "Tap this row", selection: { [unowned self] in
                    
                    //  三段式，非常完美的简单页面设置和实现
                    self.name = "新的值"
                    // 刷新界面
                    self.dataSource.sections[0].rows[0].detailText = "新的值"
                    self.tableView.reloadRowsAtIndexPaths([NSIndexPath(forRow: 0, inSection: 0)], withRowAnimation: UITableViewRowAnimation.Automatic)
                    
                    self.tableView.reloadData()
                    self.showAlert(title: "Row Selection")
                }),
                Row(text: "Tap this row", selection: { [unowned self] in
                    let viewController = ViewController()
                    self.navigationController?.pushViewController(viewController, animated: true)
                })
                ], footer: TBSection.Extremity.View(emptyHeaderFooter)),
            
            // 全部用了 [unowned self] 这个东西强大啊
            TBSection(header: TBSection.Extremity.View(emptyHeaderFooter), rows: [
                Row(text: "Swipe this row", editActions: [
                    Row.EditAction(title: "Warn", backgroundColor: .orangeColor(), selection: { [unowned self] in
                        self.showAlert(title: "Warned.")
                    }),
                    Row.EditAction(title: "Delete", style: .Destructive, selection: { [unowned self] in
                        self.showAlert(title: "Deleted.")
                    })
                ])
            ])
        ]
    }


    // MARK: - Private

    private func showAlert(title title: String? = nil, message: String? = "You tapped it. Good work.", button: String = "Thanks") {
        let alert = UIAlertController(title: title, message: message, preferredStyle: .Alert)
        alert.addAction(UIAlertAction(title: button, style: .Cancel, handler: nil))
        presentViewController(alert, animated: true, completion: nil)
    }
}
